//
//  HLCourseDetailViewController.m
//  HLAerospace
//
//  Created by hulian-ios on 17/9/7.
//  Copyright © 2017年 hulian-LB. All rights reserved.
//

#import "HLCourseFoldViewController.h"
#import "MBProgressHUD+HL.h"
#import "HttpRequestManager.h"
#import <MJExtension/MJExtension.h>
#import "HLUserInfo.h"
#import "HLFoldList.h"
#import "HLFoldCourse.h"
#import "HLFoldContent.h"
#import "HLFoldSectionView.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "HLFoldCourseCell.h"
#import "HLMoreCourse.h"
#import "HLCourseDetailViewController.h"
#import "HLArticleViewController.h"

#define kTableViewRowHeight IS_IPHONE_5 ? 44 : 50

@interface HLCourseFoldViewController ()<UITableViewDelegate,UITableViewDataSource,HLFoldSectionViewDelegate>
{
    NSArray *_requestData;
}
@property(nonatomic,strong)NSMutableDictionary *showDic;
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic ,strong) UIImageView *headImageView;

/** 数据源  */
@property (nonatomic ,strong) NSMutableArray *dataSource;

@end

@implementation HLCourseFoldViewController

- (NSMutableDictionary *)showDic
{
    if (!_showDic) {
        _showDic = [NSMutableDictionary dictionary];
    }
    return _showDic;
}

- (NSMutableArray *)dataSource
{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self loadRequest];
    
    [self setUI];
}

- (void)setUI
{
    self.navigationItem.title = @"航天课件";
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = HLRGB(240, 240, 240);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[HLFoldCourseCell class] forCellReuseIdentifier:@"HLFoldCourseCell"];
    
    UIImageView *headImageView = [[UIImageView alloc] init];
    headImageView.height = ScreenWidth * 9 / 16;
    self.headImageView = headImageView;
    self.tableView.tableHeaderView = headImageView;
}

- (void)loadRequest
{
    [MBProgressHUD show];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"sign"] = [kHLRequestGetCourseList signStr];
    params[@"course_id"] = self.course.ID;
    __weak typeof(self) weakSelf = self;
    [[HTTPRequestManager defaultManager] HTTPGetRequestWithApiName:kHLRequestGetCourseList parameters:params success:^(NSURLSessionDataTask * _Nonnull dataTask, id  _Nonnull responseObject){
        [MBProgressHUD hideHUD];
        if ([responseObject[@"code"] integerValue] == 200) {
            __strong typeof(weakSelf) strongSelf = weakSelf;
            NSMutableArray *tempDatas = [NSMutableArray array];
            tempDatas = [HLFoldList mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"list_main"]];
            /**** 顶部设置图片 ****/
            [strongSelf.headImageView sd_setImageWithURL:[NSURL URLWithString:responseObject[@"data"][@"pic"]] placeholderImage:[UIImage imageNamed:@"lagou2"]];
            _requestData = tempDatas;
        
            [strongSelf _dealWithDatas:tempDatas];
            [strongSelf.tableView reloadData];
            
        }else{
            
        }
    } failure:^(NSURLSessionDataTask * _Nonnull dataTask, NSError * _Nonnull error) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showErrorMessage:@"网络超时"];
    }];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *a = self.dataSource[section];
    return a.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataSource.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HLFoldCourseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HLFoldCourseCell"];
    cell.foldCourse = self.dataSource[indexPath.section][indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.showDic[[NSString stringWithFormat:@"%ld",indexPath.section]]) {
        HLFoldCourse *couse = self.dataSource[indexPath.section][indexPath.row];
        if (couse.isFirst) {
            return IS_IPHONE_5 ? 64 : 70;
        }else{
            return IS_IPHONE_5 ? 44 : 50;
        }
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return kTableViewRowHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 1;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{    
    HLFoldSectionView *view = [[HLFoldSectionView alloc] init];
    view.frame = CGRectMake(0, 0, ScreenWidth, kTableViewRowHeight);
    view.foldList = _requestData[section];
    view.delegate  = self;
    view.isUnFold = self.showDic[[NSString stringWithFormat:@"%ld",section]];
    return view;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HLFoldCourse *course = self.dataSource[indexPath.section][indexPath.row];
    if ([course.type isEqualToString:@"1"]) {
        HLCourseDetailViewController *couseDetail = [HLCourseDetailViewController new];
        couseDetail.foldCourse = course;
        couseDetail.moreCourse = self.course;
        [self.navigationController pushViewController:couseDetail animated:YES];
    }
    if ([course.type isEqualToString:@"2"]) {
        HLArticleViewController *article = [HLArticleViewController new];
        article.foldCourse = course;
        article.moreCourse = self.course;
        [self.navigationController pushViewController:article animated:YES];
    }
}

#pragma mark - HLFoldSectionViewDelegate

- (void)foldSectionView:(HLFoldSectionView *)foldSectionView didSelectedWith:(HLFoldList *)foldList
{
    NSInteger index = [_requestData indexOfObject:foldList];
    NSString *key = [NSString stringWithFormat:@"%ld",index];
    if (![_showDic objectForKey:key]) {
        [_showDic setObject:@"isSelected" forKey:key];
    }else{
        [_showDic removeObjectForKey:key];
    }
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:index] withRowAnimation:UITableViewRowAnimationFade];
}

#pragma mark - Private

/**
 处理服务器返回的数据

 @param temp 服务器返回的数据
 */
- (void)_dealWithDatas:(NSMutableArray *)temp
{
    for (HLFoldList *list in temp) {
        NSMutableArray *tempArray = [NSMutableArray array];
        for (HLFoldContent *content in list.list) {
            /**** 是否需要标题的cell ****/
            if (content.content.count == 0) {
                continue;
            }
            HLFoldCourse *couese = content.content[0];
            couese.headTitleText = content.list_name;
            couese.isFirst = YES;
            [tempArray addObjectsFromArray:content.content];
        }
        HLFoldCourse *course = tempArray.lastObject;
        course.isHeadLine = YES;
        [self.dataSource addObject:tempArray];
    }
    
}


@end


